package com.hongxun.budget.ui.fragment.ys;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.EProblemexistEntityDao;
import com.hongxun.budget.ui.activity.ys.EditeYsActivity;
import com.hongxun.budget.ui.dialog.NoticeDialog;
import com.hongxun.budget.ui.fragment.BaseListFragment;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.DateUtils;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.entity.LoginInfo;
import com.hongxun.greenentity.ys.EProblemEntity;
import com.hongxun.greenentity.ys.EProblemexistEntity;
import com.hongxun.interfaces.ClickListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import butterknife.OnClick;

/**
 * Created by admin on 2018/9/19.
 */

public class YsFragment extends BaseListFragment {

    List<EProblemexistEntity> datas = new ArrayList<>();
    List<EProblemexistEntity> datasDelete = new ArrayList<>();
    SuperAdapter<EProblemexistEntity> adapter;

    @Override
    protected int getMyListLayoutId() {
        return R.layout.fragment_ys;
    }

    @Override
    public SuperAdapter getAdapter() {
        return adapter = new SuperAdapter<EProblemexistEntity>(R.layout.content_ys) {
            @Override
            public void bindView(ViewHolder viewHolder, final EProblemexistEntity e, final int i) {
                ((TextView) viewHolder.getView(R.id.process)).setText(TextUtils.isEmpty(e.getHandler()) ? "未处理" : "已处理");
                ((TextView) viewHolder.getView(R.id.problem)).setText(e.getCommon());
                ((TextView) viewHolder.getView(R.id.description)).setText(e.getAnalysis());
                ((TextView) viewHolder.getView(R.id.time)).setText(DateUtils.getDateToStringNoHH(e.getFindtime()));
                Button button = viewHolder.getView(R.id.ys);
                if (TextUtils.isEmpty(e.getAccepter())) {
                    button.setClickable(true);
                    button.setText("验收");
                    button.setBackgroundResource(R.drawable.shape_btn_nor_a);
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            e.setAccepter(LoginInfo.getInfo(context).getUsername());
                            e.setAccepttime(new Date());
                            adapter.notifyDataSetChanged();
                        }
                    });
                } else {
                    button.setClickable(false);
                    button.setText("已验收");
                    button.setBackgroundResource(R.drawable.shape_btn_nor_g);
                }
                viewHolder.getView(R.id.layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, EditeYsActivity.class);
                        intent.putExtra("problem", e);
                        intent.putExtra("index", i);
                        startActivityForResult(intent, 999);
                    }
                });
                viewHolder.getView(R.id.layout).setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        new NoticeDialog().setMsg("是否删除该问题?").setCancelButton("取消", null).setCancelButton("删除", new ClickListener() {
                            @Override
                            public void click() {
                                datasDelete.add(e);
                                datas.remove(e);
                                adapter.notifyDataSetChanged();
                            }
                        }).show(getFragmentManager(), "");
                        return false;
                    }
                });
            }
        };
    }

    @Override
    public void initView() {
        super.initView();
        List<EProblemexistEntity> eProblemexistEntities = GreenDaoManager.getInstance().getmDaoSession().getEProblemexistEntityDao().queryBuilder()
                .where(EProblemexistEntityDao.Properties.Projectguid.eq(SPUtils.get(context, "project_ys_guid", ""))
                        , EProblemexistEntityDao.Properties.Pointguid.eq(SPUtils.get(context, "UUID", ""))
                        , EProblemexistEntityDao.Properties.Datastate.notEq("删除")
                ).list();
        datas.addAll(eProblemexistEntities);
        adapter.setData(datas);
        adapter.setEmptyDataView(R.layout.view_empty);
    }

    public void insertDB() {
        GreenDaoManager.getInstance().getmDaoSession().getEProblemexistEntityDao().insertOrReplaceInTx(datas);
        for (EProblemexistEntity e : datasDelete) {
            e.setDatastate("删除");
        }
        GreenDaoManager.getInstance().getmDaoSession().getEProblemexistEntityDao().updateInTx(datasDelete);
    }

    @OnClick(R.id.add_button)
    public void onViewClicked() {
        DBUtils.addProblem(this, 1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && data != null) {
            if (requestCode == 1) {
                EProblemEntity b = (EProblemEntity) data.getSerializableExtra("problem");
                setD(b);
            } else if (requestCode == 999) {
                int index = data.getIntExtra("index", 0);
                datas.remove(index);
                EProblemexistEntity b = (EProblemexistEntity) data.getSerializableExtra("problem");
                datas.add(index, b);
                adapter.notifyDataSetChanged();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void setD(EProblemEntity b) {
        EProblemexistEntity e = new EProblemexistEntity();
        e.setGuid(UUID.randomUUID().toString());
        e.setProjectguid(SPUtils.get(context, "project_ys_guid", "").toString());
        e.setPointguid(SPUtils.get(context, "UUID", "").toString());
        e.setDatastate("新增");
        e.setBigtype(b.getBigtype());
        e.setSmalltype(b.getSmalltype());
        e.setCommon(b.getCommon());
        e.setAnalysis(b.getAnalysis());
        e.setProblemguid(b.getGuid());
        e.setFinder(LoginInfo.getInfo(context).getUsername());
        e.setNote(b.getMeasures());
        e.setFindtime(new Date());
        datas.add(e);
        adapter.notifyDataSetChanged();
    }
}
